package ee.tlnk.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;

import ee.tlnk.dao.AccountDAO;
import ee.tlnk.data.Account;
import ee.tlnk.model.AccountModel;

/**
 * The Class AccountRestService.
 */
@Path("/accounts")
public class AccountRestService {


    @Autowired
    private AccountDAO accountDAO;

    
    private static Map<Integer, Account> accounts = new HashMap<Integer, Account>();

    /**
    	 * List accounts.
    	 *
    	 * @return the list
    	 */
    static {
    	Account account1 = new Account();
    	account1.setId(1);
    	account1.setfName("Alexander");
    	account1.setPosition("Software Engineer");
    	accounts.put(account1.getId(), account1);

        Account account2 = new Account();
        account2.setId(2);
        account2.setfName("John");
        account2.setPosition("Business Analyst");
        accounts.put(account2.getId(), account2);
    }

    	@GET
	    @Path("/accounts")
	    @Produces("application/xml")
	    public List<Account> listAccounts() {
    		List<Account> a = new ArrayList<Account>();
     		List<AccountModel> accountsModel = accountDAO.listAccount();
     		for (AccountModel accountModel : accountsModel) {
				Account account = new Account();
				account.setId(accountModel.getId());
				account.setfName(accountModel.getFirstname());
				account.setsName(accountModel.getSurname());
				account.setPosition(accountModel.getPosition());
				a.add(account);
			}
	        return a;
	    }

    	@GET
	    @Path("/static")
	    @Produces("application/xml")
	    public List<Account> listStaticAccounts() {
                return new ArrayList<Account>(accounts.values());
	    }
    	
    	
        @GET
        @Path("/hello")
        @Produces("text/plain")
        public String hello() {
            return "Hello World";
        }

        /**
         * Echo.
         *
         * @param message the message
         * @return the string
         */
        @GET
        @Path("/echo/{message}")
        @Produces("text/plain")
        public String echo(@PathParam("message") String message) {
            return message;
        }

 

}
